package com.chen.back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chen.back.entity.*;
import com.chen.back.mapper.*;
import com.chen.back.service.RoleService;
import com.chen.back.service.UserRoleService;
import com.chen.back.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.back.util.DataUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chen
 * @since 2022-02-24
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
@Autowired
private UserRoleService userRoleService;
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private ChooseCourseMapper chooseCourseMapper;
    @Override
    public User getUserByUsername(String usernmae) {
        return userMapper.getUserByUsername(usernmae);
    }

    @Override
    public List<Map<String, Object>> getMenuList(String username) {
        List<Map<String, Object>> maps = new ArrayList<>();
        //根据姓名得到所有权限为1的菜单
        List<Permission> list = permissionMapper.selectFatherPermissionByUsername(username);
        for (Permission s : list) {
            //再将菜单中子菜单放到map中
            Map<String, Object> map = new HashMap<>();
            map.put("menu", s);
            //在父菜单中将子菜单放到sublist中
            List<Permission> subList = permissionMapper.selectSubPermissionByFatherId(s.getPermissionId());
            map.put("subMenu", subList);
            maps.add(map);
        }
        return maps;
    }

    //PageInfo是封装信息
    @Override
    public Map<String, Object> lookPerson(String username, String roleName, Integer pageNum, Integer pageSize) {
        int first = pageNum == null ? 1 : pageNum;//如果pageNum不是空就赋值给first
        int second = pageSize == null ? 4 : pageSize;
        PageHelper.startPage(first, second);
        List<User> userList = userMapper.lookPerson(username, roleName);//用户的身份和名字才能确定一个人
        Map<String, Object> map = new HashMap<>();
        PageInfo pageInfo = new PageInfo(userList);
        map.put("list", userList);

        map.put("total", pageInfo.getTotal());
        return map;
    }
    //根据课程的id找到该科目下的所有学生
    @Override
    public List<User> getStudentListByCourseId(Integer courseId) {
        return userMapper.getStudentListByCourseId(courseId);
    }
//    根据课程id得到学生的成绩
@Override
public List<Map<String, Object>> getStudentsGradeByCourseId(Integer courseId) {
    List<Map<String, Object>> maps = new ArrayList<>();
    List<User> users = userMapper.getStudentListByCourseId(courseId);
    for (User user : users) {
//使用条件构造器用户的id与科目的id都相等
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", user.getUserId());
        queryWrapper.eq("course_id", courseId);
//        满足两个条件才能得到科目
        ChooseCourse chooseCourse = chooseCourseMapper.selectOne(queryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("student", user);
        map.put("grade", chooseCourse);
        maps.add(map);
    }
    return maps;
}

//通过用户的学院id来来查找该学院所有的老师名单
@Override
public List<User> getAllTeachersBySchoolId(Integer schoolId) {
    return userMapper.getAllTeachersBySchoolId(schoolId);
}

    @Override
    public BarVO barVOList() {
        BarVO barVO =new BarVO();
        List<String> names=new ArrayList<>();
        List<DataVO> values = new ArrayList<>();
        //先查数据所有
        List<Role> roles = roleMapper.selectList(null);

        for (Role role: roles) {
//           根据用户名字得到横坐标
            names.add(role.getRoleName());
            DataVO dataVO=new DataVO();
            Integer count = userRoleMapper.selectCount(new QueryWrapper<UserRole>().eq("role_id", role.getRoleId()));
            dataVO.setValue(count);
            dataVO.setItemStyle(DataUtil.createItemStyle(count));
            values.add(dataVO);

        }
        barVO.setNames(names);
        barVO.setValues(values);
        return barVO;
    }
    @Override
    public List<PieVO> pieList() {
        List<PieVO> pieVOList=new ArrayList<>();

        List<Role> roles = roleMapper.selectList(null);

        for (Role role:roles) {
            PieVO pieVO = new PieVO();
            Integer count = userRoleMapper.selectCount(new QueryWrapper<UserRole>().eq("role_id", role.getRoleId()));
            pieVO.setValue(count);
            pieVO.setName(role.getRoleName());

            pieVO.setItemStyle(DataUtil.createItemStyle(count));
            pieVOList.add(pieVO);

        }

        return pieVOList;
    }


}
